package com.example.mp;

import com.example.mp.mapper.UserMapper;
import com.example.mp.pojo.User;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * @BelongsProject: MP
 * @BelongsPackage: com.example.mp
 * @Author: wyj
 * @CreateTime: 2024-05-14  15:02
 * @Description:
 * @Version: 1.0
 */
@SpringBootTest
public class MPTestMapper {

    @Autowired
    private UserMapper userMapper;


    @Test
    public void testList() {
        userMapper.selectList(null).forEach(System.out::println);
    }

    @Test
    public void testInsert() {
        // sql: insert into user ( id , name, age, email) values (? ,?, ?, ?)
        User user = new User();
        user.setName("张三4");
        user.setAge(23);
        user.setEmail("zhangsan@email.com");
        int insert = userMapper.insert(user);
        System.out.println(insert);

        // 获取自动主键
        System.out.println(user.getId());
    }

    @Test
    public void testDeleteById() {
        // 通过ID 删除用户信息
        // sql : delete from user where id = ?
        int result = userMapper.deleteById(1794029037574737922L);
        System.out.println(result);
    }

    @Test
    public void testDeleteByMap() {
        // 根据map 条件删除
        // sql : delete from user where name = ? and age = ?
        // key 完全和表字段对应 不是对象属性名
        Map<String, Object> map = new HashMap<>();
        map.put("name", "张三");
        map.put("age", 23);
        int result = userMapper.deleteByMap(map);
        System.out.println(result);
    }

    @Test
    public void testDeleteByBatchIds() {
        // 根据ID 批量删除
        // sql : delete from user where id in ( ? , ? , ?)
        int result = userMapper.deleteBatchIds(Arrays.asList(1L, 2L, 3L));
        System.out.println(result);
    }

    @Test
    public void testUpdate() {
        // 更新数据
        // sql : update user set name = ? , age = ? , email = ? where id = ?
        // 当user 对象某个属性为null 时 不会修改该字段
        User user = new User();
        user.setId(4L);
        user.setName("李四");
        user.setEmail("LISI@EMAIL.COM");
        System.out.println(user);
        int update = userMapper.updateById(user);
        System.out.println(update);
    }

    @Test
    public void testSelectById() {
        User user = userMapper.selectById(1L);
        System.out.println(user);
    }

    @Test
    public void testSelectBatchIds() {
        // 根据ID 批量查询
        // sql : select * from user where id in ( ? , ? , ?)
        userMapper.selectBatchIds(Arrays.asList(1L, 2L, 3L)).forEach(System.out::println);
    }

    @Test
    public void testSelectByMap() {
        // 根据map 查询
        // sql : select * from user where name = ? and age = ?
        // 根据map 的条件进行查询 key 要完全和表字段相同 不是和对象属性相同
        Map<String, Object> map = new HashMap<>();
        map.put("name", "JACK");
        map.put("age", 23);
        userMapper.selectByMap(map).forEach(System.out::println);
    }

    @Test
    public void testSelectList() {
        // 查询所有
        userMapper.selectList(null).forEach(System.out::println);
    }

    @Test
    public void testSelectMapByUserMapper() {
        Map<String, Object> map = userMapper.selectMapById(1L);
        System.out.println(map);
    }
}
